<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('product/product_model');
    }

    function index(){
        $category_data = $this->product_model->get_category();
        $category[0] = '';
        foreach (@$category_data as $key => $value){
            $category[$value['category_id']] = $value['title'].' ('.$value['discount'].'%)';
        }

        $manufacturer_data = $this->product_model->get_manufacturer();
        foreach($manufacturer_data as $key => $value){
            $manufacturer[$value['manufacture_id']] = $value['title'];
        }

        $data['category'] = @$category;
        $data['manufacturer'] = @$manufacturer;
        $data['result'] = $this->product_model->get_product('', '', '', '', '', '', '');
        $data['tpl'] = 'admin/product/index';
        $this->load->view('_layout/admin', $data);
    }

    function add(){
        $this->update();
    }

    function edit($id){
        $this->update($id);
    }

    function update($product_id = 0){
        $product_id = intval($product_id);
        $info = array();

        if ($_POST){
            $info = array(
                'product_id'           => $this->input->post('product_id'),
                'title'                => $this->input->post('title'),
                'category_id'          => $this->input->post('category'),
                'manufacture'          => $this->input->post('manufacture'),
                'intro'                => trim($this->input->post('intro')),
                'description'          => trim($this->input->post('description')),
                'price_old'            => only_numeric($this->input->post('price_old')),
                'price_new'            => only_numeric($this->input->post('price_new')),
                'discount'             => floatval($this->input->post('discount')),
                'quantity'             => only_numeric($this->input->post('quantity')),
                'color'                => trim($this->input->post('color')),
                'material'             => trim($this->input->post('material')),
                'width'                => trim($this->input->post('width')),
                'height'               => trim($this->input->post('height')),
                'weight'               => trim($this->input->post('weight')),
                'depth'                => trim($this->input->post('depth')),
                'size'                 => trim($this->input->post('size')),
                'update_date'          => date('Y-m-d H:i:s', time()),
                'draft'                => 'no',
                'active_flag'          => ACTIVE_FLAG
            );

            $result = $this->product_model->update($info);
            if (!is_bool($result)){
                $data['msg'] = 'Thông tin sản phẩm đã được lưu thành công';
            }
        }

        //Check draft
        if ($product_id == 0){
            $check_draft = $this->product_model->check_draft();
            if (is_bool($check_draft)){
                $this->db->insert(TBL_PRODUCT, array('draft' => 'yes', 'create_date' => date('Y-m-d H:i:s')));
                $product_id = $this->db->insert_id();
            } else {
                $data['data'] = $check_draft;
                $product_id = $check_draft['product_id'];
            }
        }

        if (!isset($data['data'])){
            $data['data'] = $this->product_model->get_product($product_id);
        }
        $data['images'] = $this->product_model->get_images($product_id);

        #$this->output->enable_profiler(true);
        #die();

        $category = $this->product_model->get_category();
        $data['category'] = @$category;

        $manufacturer_data = $this->product_model->get_manufacturer();
        foreach($manufacturer_data as $key => $value){
            $manufacturer[$value['manufacture_id']] = $value['title'];
        }
        $data['manufacturer'] = @$manufacturer;

        $data['tpl'] = 'admin/product/edit';
        $this->load->view('_layout/admin', $data);
    }

    function delete(){
        $id = $this->input->post('id');
        if (!is_array($id)){
            $id = array($id);
        }
        $result = $this->product_model->_delete_product($id);
        echo $result;
    }

    function calc(){
        $html    = '<table border="1" cellspacing="0" cellpadding="5" width="100%">';
        $html .= '<tr>';
        $html .= '<th width="30">ID</th>';
        $html .= '<th>Product name</th>';
        $html .= '<th width="80">Giảm SP (%)</th>';
        $html .= '<th width="150">Danh mục</th>';
        $html .= '<th width="100">Giảm DM (%)</th>';
        $html .= '<th width="70">Giá gốc</th>';
        $html .= '<th width="70">Giá giảm</th>';
        $html .= '</tr>';

        $result  = $this->db
        ->select('`p`.`product_id`, `p`.`title`, p.price_old, p.price_new, p.discount as p_discount, c.discount as c_discount, c.title as category')
        ->from(TBL_PRODUCT. ' p')
        ->join(TBL_PRODUCT_CATEGORY.' c', 'p.category_id = c.category_id', 'left')
        //->where('c.discount >', 0)
        ->where('p.draft', 'no')
        ->where('p.delete_flag', 0)
        ->get()
        ->result_array();
        foreach ($result as $key => $value) {
            $price_new = $value['price_old'] * (1 - $value['p_discount']/100 -  $value['c_discount']/100);
            $this->db->where('product_id', $value['product_id'])->update(TBL_PRODUCT, array('price_new' => $price_new));
            $html .= '<tr>';
            $html .= '<td align="center">'.$value['product_id'].'</td>';
            $html .= '<td>'.$value['title'].'</td>';
            $html .= '<td align="right">'.$value['p_discount'].'</td>';
            $html .= '<td>'.$value['category'].'</td>';
            $html .= '<td align="right">'.$value['c_discount'].'</td>';
            $html .= '<td align="right">'.number_format($value['price_old'], 0).'</td>';
            $html .= '<td align="right">'.number_format($price_new, 0).'</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        $data['result'] = $html;
        $data['tpl'] = 'admin/product/calc';
        $this->load->view('_layout/admin', $data);
    }
}